/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processer;

import connection.IMessageListenner;
import log.LogDef;
import main.BotManager;
import main.Request;
import utils.Debug;

/**
 *
 * @author Administrator
 */
public class RoBot implements IMessageListenner, Runnable {

    public static final int LOGGER = 1;
    public static final int GAME_CHOOSER = 2;
    public static final int TABLE_CHOOSER = 3;
    public static final int ROOM_CHOOSER = 4;
    public static final int WAITING = 5;
    public static final int PLAYING_PHOM = 6;
    public static final int PLAYING_TIEN_LEN = 7;
    public static final int PLAYING_BA_CAY = 8;
    private boolean isRunning;
    private boolean isConnected;
    private int pingId;
    private int roomId;
    private int tableId;
    protected Request request;
    private GameData gameData;
    protected String userName;
    protected String passWord;
    private Processer processer;
    private Logger logger;
    private String host;
    private String port;
    private BotManager manager;

    public RoBot(int id, String userName, String pass, int roomId, int tableId, String host, String port) {
        this.pingId = id;
        this.userName = userName;
        this.passWord = pass;
        this.roomId = roomId;
        this.tableId = tableId;
        this.host = host;
        this.port = port;
        initValue();
        initObject();
    }

    public void setManager(BotManager manager) {
        this.manager = manager;
    }

    public RoBot(int id, String userName, String pass, String host, String port) {
        Debug.d("new robot");
        this.pingId = id;
        this.userName = userName;
        this.passWord = pass;
        this.host = host;
        this.port = port;
        initValue();
        initObject();
    }

//    public void setGameType(int gameType) {
//        gameData.gameTypePlay = gameType;
//    }
    private void initObject() {
        gameData = new GameData();
        gameData.setRoomId(roomId);
        gameData.setTableId(tableId);
        initProcesser();
        request = new Request();
    }

    private void initProcesser() {
        logger = new Logger(this, gameData, manager);
        processer = logger;
    }

    private void initValue() {
    }

    private void initConnection() {
        request.initializeConnection(host, port);
        request.setMessageListenner(this);
        isConnected = request.isConnect();
        Debug.d("is connect :" + isConnected);
        if (isConnected) {
            request.login(userName, passWord);
        } else {
            stop();
        }
    }

    public void start() {
        isRunning = true;
        new Thread(new Runnable() {

            @Override
            public void run() {
                initConnection();
            }
        }).start();
        new Thread(this).start();
    }
//--------------------------------------------------------------------------

    protected void changeProcesser(int type) {
        switch (type) {
            case LOGGER:
                processer = new Logger(this, gameData, manager);
                break;
            case GAME_CHOOSER:
                processer = new GameChooser(this, gameData, manager);
                break;
            case TABLE_CHOOSER:
                processer = new TableChooser(this, gameData, manager);
                break;
            case ROOM_CHOOSER:
                processer = new RoomChooser(this, gameData, manager);
                break;
            case WAITING:
                processer = new Waiter(this, gameData, manager);
                break;
            case PLAYING_PHOM:
                processer = new PhomPlayer(this, gameData, manager);
                break;
            case PLAYING_TIEN_LEN:
                processer = new TienlenPlayer(this, gameData, manager);
                break;
            case PLAYING_BA_CAY:
                processer = new BaCayPlayer(this, gameData, manager);
                break;
        }
    }
//-----------------------------------------------------------------------------------

    @Override
    public void processMessage(String[] part) {
        processer.processMesage(part);
    }

    protected void print() {
    }

    @Override
    public void run() {
        while (isRunning) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            processer.doLoop();
        }
    }

    @Override
    public void stop() {
        isRunning = false;
        manager.kickUser();
        manager.logUserStatus(userName + " ,stop " + pingId, LogDef.LOG_LEVEL.LOG_INFO);
        Debug.d("stop");
    }
}
